from widgets.scriptWidgets.ScriptWidget import ScriptWidget
from enums.WidgetType import WidgetType
from PySide6.QtWidgets import QLabel, QPushButton
from ..ScriptInput import ScriptInput
from util.Mouse import MouseUtil


class WaitText(ScriptWidget):
    def __init__(self, initType=None):
        super(WaitText, self).__init__(WidgetType.WAIT_TEXT)

        self.labTitle = QLabel('等待文字:', self)
        self.labTitle.setFixedSize(55, self.lineHeight)
        self.addWidget(self.labTitle)

        self.newLineSpaceWidth = self.labTitle.width() + self.intervalX

        # x
        self.inputX = ScriptInput(self, 'x')
        self.inputX.text = 0
        self.addWidget(self.inputX)

        # y
        self.inputY = ScriptInput(self, 'y')
        self.inputY.text = 0
        self.addWidget(self.inputY)

        if initType == 'edit' or initType == 'create':
            self.addOperate()

        self.newLine()
        # 宽
        self.inputWidth = ScriptInput(self, '宽')
        self.inputWidth.text = 0
        self.addWidget(self.inputWidth)

        # 高
        self.inputHeight = ScriptInput(self, '高')
        self.inputHeight.text = 0
        self.addWidget(self.inputHeight)

        self.btnTrack = QPushButton(self)
        self.btnTrack.setText('选择区域')
        self.btnTrack.clicked.connect(self.chooseArea)
        self.addWidget(self.btnTrack)

        # 文字
        self.inputText = ScriptInput(self, '文字', 16, 'string')
        self.addWidget(self.inputText)

    def onMouseClick(self, x, y, width, height, button):
        self.inputX.text = x
        self.inputY.text = y
        self.inputWidth.text = width
        self.inputHeight.text = height

    def chooseArea(self):
        MouseUtil.listenArea(self.onMouseClick)

    @property
    def data(self):
        return {
            "type": self.type.value,
            'x': int(self.inputX.text),
            'y': int(self.inputY.text),
            'width': int(self.inputWidth.text),
            'height': int(self.inputHeight.text),
            'text': self.inputText.text,
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        self.inputX.text = str(data['x'])
        self.inputY.text = str(data['y'])
        self.inputWidth.text = str(data['width'])
        self.inputHeight.text = str(data['height'])
        self.inputText.text = data['text']
